package com.kang.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.kang.pojo.AjaxResult;
import com.kang.pojo.Warehouse;
import com.kang.service.WarehouseService;
import com.vonechina.entity.page.WarehousePage;

/**
 * 仓库控制层
 * 
 * @author zs614
 *
 */
@Controller
@RequestMapping("warehouse")
@SessionAttributes({ "warehousePage" })
public class WarehouseController extends com.kang.controller.Controller {
	/**
	 * 仓库服务层
	 */
	@Resource
	private WarehouseService warehouseService;

	/**
	 * 分页查询仓库页面
	 * 
	 * @param session
	 * @param model
	 * @param warehousePage
	 *            搜索按钮自动封装
	 * @return
	 */
	@RequestMapping("listWarehouse")
	public String listWarehouse(HttpSession session, Model model, WarehousePage warehousePage) {
		initRight(session);
		if (checkRight("查看仓库") != SUCCESS) {
			return "basic/noRight";
		}
		model.addAttribute("isAdd", checkRight("新增仓库") == SUCCESS);
		model.addAttribute("isEdit", checkRight("编辑仓库") == SUCCESS);
		model.addAttribute("isDelete", checkRight("删除仓库") == SUCCESS);
		List<Warehouse> warehouseList = warehouseService.listWarehouse(warehousePage);
		model.addAttribute("warehouseList", warehouseList);
		warehousePage.setPageUrl("warehouse/listWarehouse.do");
		model.addAttribute("page", warehousePage);
		return "basicData/warehouseManage/listWarehouse";
	}

	/**
	 * 新增仓库
	 * 
	 * @param session
	 * @param warehouse
	 *            仓库实体类自动封装
	 * @return 返回json字符串
	 */
	@RequestMapping("add")
	@ResponseBody
	public String addwarehouse(HttpSession session, Warehouse warehouse) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("新增仓库") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		warehouse.setWarehousecode(warehouseService.generatoCode());
		warehouse.setCreatedate(new Date());
		warehouse.setCreatepersonid((Integer) session.getAttribute("userID"));
		int a = warehouseService.insertSelective(warehouse);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}

	/**
	 * 编辑仓库信息
	 * 
	 * @param session
	 * @param warehouse
	 *            仓库实体类自动封装
	 * @return 返回json字符串
	 */
	@RequestMapping("update")
	@ResponseBody
	public String updatewarehouse(HttpSession session, Warehouse warehouse) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("编辑仓库") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		int a = warehouseService.updateByPrimaryKeySelective(warehouse);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}

	/**
	 * 根据仓库ID删除仓库
	 * 
	 * @param session
	 * @param warehousemanagementid
	 *            仓库ID
	 * @return 返回json字符串
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String deletewarehouse(HttpSession session, Integer warehousemanagementid) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if (checkRight("删除仓库") != SUCCESS) {
			result.setResult("noRight");
			return JSON.toJSONString(result);
		}
		int a = warehouseService.deleteByPrimaryKey(warehousemanagementid);
		if (a > 0) {
			result.setResult("success");
		}
		return JSON.toJSONString(result);
	}

	/**
	 * 根据仓库ID查询仓库信息
	 * 
	 * @param session
	 * @param warehousemanagementid
	 *            仓库ID
	 * @return 返回json字符串
	 */
	@RequestMapping("findWarehouseById")
	@ResponseBody
	public String findWarehouseById(HttpSession session, Integer warehousemanagementid) {
		Warehouse warehouse = warehouseService.selectByPrimaryKey(warehousemanagementid);
		return JSON.toJSONString(warehouse);
	}

}
